﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace ProjectManApi.Extensions
{
    public static class SwaggerExtension
    {
        public static void ConfigureSwagger(this IServiceCollection services)
        {
            services.AddSwaggerGen(c =>
            {
                //1 SwaggerDoc
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "接口文档",
                    Description = "大三资CoreWebApi1.3"
                });


                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                //var xmlFile1 = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath1 = Path.Combine(AppContext.BaseDirectory, "BusinessEntity.xml");
                // 添加控制器层注释，true表示显示控制器注释
                if (File.Exists(xmlPath))
                    c.IncludeXmlComments(xmlPath, true);
                //c.IncludeXmlComments(xmlPath1, true);



                //注入汉化文件
                //c.InjectOnCompleteJavaScript($"/swagger_translator.js");

                //2 这里是knife4j ui
                //c.AddServer(new OpenApiServer()
                //{
                //    Url = "",
                //    Description = "vvv"
                //});
                //c.CustomOperationIds(apiDesc =>
                //{
                //    var controllerAction = apiDesc.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;
                //    return controllerAction.ControllerName + "-" + controllerAction.ActionName;
                //});

                //var filePath = Path.Combine(System.AppContext.BaseDirectory, "Knife4jUIDemo.xml");
                //c.IncludeXmlComments(filePath, true);

                //这里是knife4j ui


                //swagger中控制请求的时候发是否需要在url中增加accesstoken
                //1.这里是Header加一个Token 开头
                //c.OperationFilter<AddAuthTokenHeaderParameter>();

                //2.这里是Header加一个Authorization Bearer 开头
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme()
                {
                    Description = "文本框里输入从服务器获取的Token。格式为：Bearer+空格+token",//JWT授权(数据将在请求头中进行传输) 参数结构: \"Authorization: Bearer {token}\"
                    Name = "Authorization",////jwt默认的参数名称
                    In = ParameterLocation.Header,////jwt默认存放Authorization信息的位置(请求头中)
                    Type = SecuritySchemeType.ApiKey,
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    { new OpenApiSecurityScheme
                    {
                        Reference = new OpenApiReference()
                        {
                            Id = "Bearer",
                            Type = ReferenceType.SecurityScheme
                        }
                    }, Array.Empty<string>() }
                });
                c.OperationFilter<SwaggerFileUploadFilter>();
            });
        }
    }
}
